package models

import (
	"database/sql"

	log "../lib/log"
)

var db *sql.DB

//Init establishes the postgres connection
//should call this function before any database operation
func Init(host string) {
	var err error
	if host != "" {
		db, err = sql.Open("postgres", host)
	} else {
		db, err = sql.Open("postgres", "postgres://Jonathan:postgres@127.0.0.1/webapi?sslmode=disable")
	}
	if err != nil {
		log.Fatal(err)
	}

	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}
	log.Success("You have connected to postgres")
}
